# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/5/10 10:57
# @Author  : qiaoc
# @File    : AuditRuleWriteOff.py
# @Desc    : 【核销单】-稽核规则

from affix import AffixUtils
import pandas as pandas
import re
from dao.PyMysql import *
import datetime


no_need_to_deal_with_benchmark_type = ['智能电表标杆（动环）', '智能电表标杆（集采）', '动环负载标杆', '平峰谷均价标杆（录平峰谷表采用总量录入方式时）']

"""
    稽核规则二：【页面稽核】
        规则2：页面【报账点缴费信息】-【实际不含税电费金额】+【实际电费税金】+【其他费用】的【含税金额】=【实际总金额】
            # ---   不一致后动作：【审核状态】-【不通过】，备注：【实际不含税电费金额】+【实际电费税金】+【其他费用】的【含税金额】不等于【实际总金额】
"""
def audit_rule_two(auditResult, audit_list, audit_refused_type, bill_number,
                   page_actual_nae_taxsad,page_actual_ef_tariff,page_amount_including_tax,
                   page_actual_total_amount):

    """
    :param auditResult:
    :param audit_list:
    :param audit_refused_type:
    :param bill_number:
    :param page_actual_nae_taxsad: 实际不含税电费金额
    :param page_actual_ef_tariff: 实际电费税金
    :param page_amount_including_tax: 总含税金额
    :param page_actual_total_amount: 实际总金额
    :return:
    """

    try:
        if page_actual_nae_taxsad and page_actual_ef_tariff and page_amount_including_tax and page_actual_total_amount:
            try:
                if round(float(str(page_actual_total_amount).strip()), 2) != round(
                        float(str(page_actual_nae_taxsad).strip()) + float(
                            str(page_actual_ef_tariff).strip()) + float(str(page_amount_including_tax)), 2):
                    auditResult.auditStatus = '不通过'  # 审核不通过
                    audit_refused_type['金额有误'] = 0
                    audit_list.append([str(bill_number), '总金额（其他费用）',
                                       str(round(float(str(page_actual_nae_taxsad).strip()) + float(
                                           str(page_actual_ef_tariff).strip()) + float(
                                           str(page_amount_including_tax)), 2)),
                                       str(page_actual_total_amount), '0', 'RPA',
                                       '【实际不含税电费金额】+【实际电费税金】+【其他费用】的【含税金额】不等于【实际报账金额】', '', '1', ''])
                else:
                    audit_list.append([str(bill_number), '总金额（其他费用）',
                                       str(round(float(str(page_actual_nae_taxsad).strip()) + float(
                                           str(page_actual_ef_tariff).strip()) + float(
                                           str(page_amount_including_tax)), 2)),
                                       str(page_actual_total_amount), '1', 'RPA', '合格', '', '1', ''])
            except Exception as e:
                # print(e)
                auditResult.auditStatus = '审核中'  # 审核不通过
                audit_refused_type['金额有误'] = 0
                audit_list.append([str(bill_number), '总金额（其他费用）',
                                   str(round(float(str(page_actual_nae_taxsad).strip()) + float(
                                       str(page_actual_ef_tariff).strip()) + float(
                                       str(page_amount_including_tax)), 2)),
                                   str(page_actual_total_amount), '0', 'RPA', 'RPA处理异常', '', '1', ''])
        else:
            auditResult.auditStatus = '审核中'  # 转人工
    except Exception as e:
        # print(e)
        auditResult.auditStatus = '审核中'  # 审核不通过
        audit_refused_type['金额有误'] = 0
        audit_list.append([str(bill_number), '总金额（其他费用）', '', '', '0', 'RPA', 'RPA处理异常', '', '1', ''])
    return auditResult,audit_list,audit_refused_type


"""
    稽核规则三：【页面稽核】
        规则3：页面【其他费】-【实际总金额】= 【实际核销金额】 +  【支付金额】
            # ---   不一致后动作：【审核状态】-【不通过】，备注：【实际总金额】不等于【实际核销金额】与【支付金额】之和
"""

def audit_rule_three(auditResult, audit_list, audit_refused_type, bill_number,
                     page_actual_total_amount,page_actual_verification_amount,page_payment_amount):
    """

    :param auditResult:
    :param audit_list:
    :param audit_refused_type:
    :param bill_number:
    :param page_actual_total_amount: 实际总金额
    :param page_actual_verification_amount: 实际核销金额
    :param page_payment_amount: 支付金额
    :return:
    """
    try:
        if page_actual_total_amount and page_actual_verification_amount and page_payment_amount:
            try:
                if round(float(str(page_actual_total_amount).strip()), 2) != round(
                        float(page_actual_verification_amount) + float(page_payment_amount), 2):
                    auditResult.auditStatus = '不通过'  # 审核不通过
                    audit_refused_type['金额有误'] = 0
                    audit_list.append([str(bill_number), '总金额（核销/支付）',
                                       str((float(page_actual_verification_amount) + float(
                                           page_payment_amount))),
                                       str(page_actual_total_amount), '0', 'RPA', '【实际总金额】不等于【实际核销金额】与【支付金额】之和',
                                       '', '1', ''])
                else:
                    audit_list.append([str(bill_number), '总金额（核销/支付）',
                                       str((float(page_actual_verification_amount) + float(
                                           page_payment_amount))),
                                       str(page_actual_total_amount), '1', 'RPA', '合格', '', '1', ''])
            except Exception as e:
                # print(e)
                auditResult.auditStatus = '审核中'  # 审核不通过
                audit_refused_type['金额有误'] = 0
                audit_list.append([str(bill_number), '总金额（核销/支付）',
                                   str((float(page_actual_verification_amount) + float(page_payment_amount))),
                                   str(page_actual_total_amount), '0', 'RPA', 'RPA处理异常', '', '1', ''])
        else:
            auditResult.auditStatus = '审核中'  # 转人工
    except Exception as e:
        # print(e)
        auditResult.auditStatus = '审核中'  # 审核不通过
        audit_refused_type['金额有误'] = 0
        audit_list.append([str(bill_number), '总金额（核销/支付）', '', '', '0', 'RPA', 'RPA处理异常', '', '1', ''])
    return auditResult, audit_list, audit_refused_type


"""
    稽核规则五：【页面稽核】
        规则5：页面【缴费标杆信息】-【标杆类型】（9类）-其中一类高于阈值（后期配置）为异常。目前为五类，预留4类，分别为智能电表标杆和动环负载标杆。
                # ---   异常后动作：检查是否存在附件5；如果附件5为单站，直接跳过（只需判断是否存在附件5）；如果附件5为汇总，进入附件6（excel）查找是否该户号；
                # ---   如果附件存在转人工（把所有的核对点都做了），如果附件不存在：【审核状态】-【不通过】，备注：请上传超标情况分析报告
"""
def audit_rule_five(auditResult, audit_list,  bill_number,
                    verification_benchmark_info_data,all_file,floder_path,affix_list_from_db,rp_code):
    try:
        exceeding_rate_error_flag = False  # 默认超标率阀值
        exceeding_rate_message = ''
        exceeding_rate_error_message = ''
        if verification_benchmark_info_data and len(verification_benchmark_info_data) > 0:
            for pbid in verification_benchmark_info_data:
                is_exceeding = pbid['is_exceeding']  # 是否超标（超标类型）
                benchmark_type_name = pbid['benchmark_type_name']  # 标杆类型名称
                # 属于四种预留类型的标杆时，跳过不处理
                if benchmark_type_name in no_need_to_deal_with_benchmark_type:
                    continue
                exceeding_rate_message = exceeding_rate_message + ',' + benchmark_type_name + ':' + str(
                    pbid['exceeding_rate'])
                if '高于上限' in str(is_exceeding):
                    exceeding_rate = str(pbid['exceeding_rate']).replace('%', '').replace('-', '0')  # 超标率
                    # if '额定功率标杆' in str(benchmark_type_name):
                    exceeding_rate_default = 0
                    # else:
                        # exceeding_rate_default = 20
                    if float(exceeding_rate) > exceeding_rate_default:
                        exceeding_rate_error_flag = True
                        exceeding_rate_error_message = exceeding_rate_error_message + ',' + benchmark_type_name + ':' + str(
                            pbid['exceeding_rate'])
            if exceeding_rate_error_flag:
                att_4_name = ''
                try:
                    auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                    att_4_name = all_file['附件5']
                    if '单站' in str(att_4_name):
                        audit_list.append(
                            [str(bill_number), '超标说明', '超标', '有附件', '1', 'RPA', '合格',
                             AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件5'), '3', ''])
                    elif '汇总' in str(att_4_name):
                        att_5_name = all_file['附件6']
                        # todo: 解析附件6中的excel，可能是压缩包，查找户号，找到则转人工，
                        try:
                            exe_data = pandas.read_excel(floder_path + '//附件6//' + att_5_name, header=1)
                            num_list = exe_data.loc[:, ['报账点编码']].dropna().values.tolist()
                        except:
                            exe_data = pandas.read_excel(floder_path + '//附件6//' + att_5_name)
                            num_list = exe_data.loc[:, ['报账点编码']].dropna().values.tolist()
                        num_flag = False
                        for num in num_list:
                            if str(num[0]).replace('.0', '') in rp_code:
                                num_flag = True
                                break
                        if num_flag:
                            audit_list.append(
                                [str(bill_number), '超标说明', '超标', '有附件', '1', 'RPA', '合格',
                                 AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件6'), '3', ''])
                        else:
                            audit_list.append(
                                [str(bill_number), '超标说明', '超标', '有附件', '0', 'RPA', '附件6找不到对应的户号',
                                 AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件6'), '3', ''])
                    else:
                        audit_list.append(
                            [str(bill_number), '超标说明', '超标', '无附件', '0', 'RPA', '不合格',
                             AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件5'), '3', ''])

                except Exception as e:
                    # print(e,e.__traceback__.tb_lineno)
                    audit_list.append(
                        [str(bill_number), '超标说明', '超标', '无附件', '0', 'RPA执行异常', '请上传超标情况分析报告',
                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件5'), '3',
                         ''])

            else:
                att_4_name = all_file['附件5']
                if att_4_name:
                    audit_list.append(
                        [str(bill_number), '超标说明', '不超标', '有附件', '1', 'RPA', '合格',
                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件5'), '3', ''])
                else:
                    audit_list.append(
                        [str(bill_number), '超标说明', '不超标', '无附件', '1', 'RPA', '合格',
                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件5'), '3', ''])
    except Exception as e:
        auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
        audit_list.append([str(bill_number), '超标说明', '', '', '0', 'RPA', 'RPA执行异常',
                           AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件5'), '3', ''])
    return auditResult, audit_list

"""
      稽核规则六：【页面稽核】
        规则6：页面【电表核销明细信息】-【本次实际分摊比例】为100%即为通过
            # ---   不一致后动作：【审核状态】-【不通过】，备注：系统中录入的分摊比例不正确
"""
def audit_rule_six(auditResult, audit_list, audit_refused_type, bill_number,whether_execute,
                                affix_list_from_db,verification_detail_info_data, page_current_share_ratio):

    """
    :param affix_list_from_db:
    :param verification_detail_info_data:
    :param page_current_share_ratio:
    :return:
    """
    if whether_execute == False:
        try:
            if page_current_share_ratio and float(str(page_current_share_ratio).strip().replace('%', '')) == 100:
                audit_list.append(
                    [str(bill_number), '分摊比例', str(page_current_share_ratio), '100', '1', 'RPA', '合格', '', '3', ''])
            else:
                auditResult.auditStatus = '不通过'  # 返回稽核结果为不通过
                audit_refused_type['信息与明细不符'] = 0
                audit_list.append(
                    [str(bill_number), '分摊比例', str(page_current_share_ratio), '100', '0', 'RPA', '系统中录入的分摊比例不正确', '',
                     '3', ''])
        except Exception as e:
            audit_list.append([str(bill_number), '分摊比例', '', '', '0', 'RPA', 'RPA执行异常',
                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件3'), '3', ''])
            auditResult.auditStatus = '审核中'  # 转人工
    else:
        for data in verification_detail_info_data:
            try:
                page_current_share_ratio = data['current_share_ratio']  # 本次实际分摊比例
                page_current_share_ratio = re.findall(r"\d+\.?\d*", str(page_current_share_ratio).replace('%', ''))[0]
                if page_current_share_ratio and float(str(page_current_share_ratio).strip().replace('%', '')) == 100:
                    audit_list.append(
                        [str(bill_number), '分摊比例', '【' + str(data['es_number']) + '】' + str(page_current_share_ratio),
                         '100', '1', 'RPA', '合格', '', '3', ''])
                else:
                    auditResult.auditStatus = '不通过'  # 返回稽核结果为不通过
                    audit_refused_type['信息与明细不符'] = 0
                    audit_list.append(
                        [str(bill_number), '分摊比例', '【' + str(data['es_number']) + '】' + str(page_current_share_ratio),
                         '100', '0', 'RPA', '系统中录入的分摊比例不正确', '',
                         '3', ''])
            except Exception as e:
                audit_list.append(
                    [str(bill_number), '分摊比例', '【' + str(data['es_number']) + '】' + '', '', '0', 'RPA', 'RPA执行异常',
                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件3'), '3', ''])
                auditResult.auditStatus = '审核中'  # 转人工
    return auditResult, audit_list, audit_refused_type
"""
     稽核规则十二：【页面稽核】
        规则12：增加“平均电费单价”展示，计算公式为：平均电费单价=【实际总金额】/【实际总耗电量】，如此值小于等于0.3或者大于等于0.7，转人工稽核。其他稽核条件均需识别判断展示。
            # ---    备注：均电费单价小于等于0.3/大于等于0.7
"""
def audit_rule_twelve(auditResult, audit_list, bill_number,
                      page_actual_reimbursement_amount,page_actual_total_pc):
    """
    :param auditResult:
    :param audit_list:
    :param bill_number:
    :param page_actual_reimbursement_amount: 实际报账金额
    :param page_actual_total_pc: 实际总耗电量
    :return:
    """
    try:
        if page_actual_reimbursement_amount and page_actual_total_pc:
            try:
                if float(page_actual_total_pc) != 0:
                    average_unit_price_of_electricity = round(
                        float(page_actual_reimbursement_amount) / float(page_actual_total_pc), 2)
                    if average_unit_price_of_electricity <= 0.3 or average_unit_price_of_electricity >= 0.7:
                        auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                        audit_list.append([str(bill_number), '平均电费单价', str(average_unit_price_of_electricity),
                                           '小于等于0.3/大于等于0.7', '0', 'RPA', '平均电费单价小于等于0.3/大于等于0.7', '', '1', ''])
                    else:
                        audit_list.append([str(bill_number), '平均电费单价', str(average_unit_price_of_electricity),
                                           '小于等于0.3/大于等于0.7', '1', 'RPA', '合格', '', '1', ''])
                else:
                    auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                    audit_list.append([str(bill_number), '平均电费单价', '0',
                                       '小于等于0.3/大于等于0.7', '0', 'RPA', '均电费单价小于等于0.3/大于等于0.7', '', '1', ''])
            except Exception as e:
                # print(e)
                auditResult.auditStatus = '审核中'  # 转人工
                audit_list.append([str(bill_number), '平均电费单价',
                                   str(float(page_actual_reimbursement_amount) / float(page_actual_total_pc)),
                                   '小于等于0.3/大于等于0.7', '0', 'RPA', 'RPA处理异常', '', '1', ''])
        else:
            auditResult.auditStatus = '审核中'  # 转人工
            audit_list.append([str(bill_number), '平均电费单价',
                               str(float(page_actual_reimbursement_amount) / float(page_actual_total_pc)),
                               '小于等于0.3/大于等于0.7', '0', 'RPA', '实际总金额或实际总耗电量为空', '', '1', ''])
    except Exception as e:
        audit_list.append([str(bill_number), '平均电费单价',
                           '',
                           '小于等于0.3/大于等于0.7', '0', 'RPA', 'RPA处理异常', '', '1', ''])
        auditResult.auditStatus = '审核中'  # 转人工
    return auditResult, audit_list
"""
    稽核规则十三：【页面稽核】
    规则13：增加“电损电量占比”展示，计算公式为：电损电量占比=【电损电量(度)】/【分摊后度数(度)】，如此值大于等于10%，转人工稽核。其他稽核条件均需识别判断展示。
    # ---    备注：电损电量占比大于等于10%
"""
def audit_rule_thirteen(auditResult, audit_list, bill_number,whether_execute,
                            page_electricity_loss,page_share_degrees,verification_detail_info_data ):
    """

    :param auditResult:
    :param audit_list:
    :param bill_number:
    :param whether_execute:
    :param page_electricity_loss: 电损电量
    :param page_share_degrees: 分摊后度数
    :param verification_detail_info_data:
    :return:
    """
    if whether_execute == False:  # 单个电表
        if page_electricity_loss and page_share_degrees:
            for page_electricity_loss_data in page_electricity_loss:
                try:
                    if float(page_share_degrees) != 0:
                        proportion_of_power_loss = round(float(page_electricity_loss_data) / float(page_share_degrees),
                                                         2)
                        if proportion_of_power_loss >= 0.1:
                            auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                            audit_list.append([str(bill_number), '电损电量占比', str(proportion_of_power_loss),
                                               str(proportion_of_power_loss), '0', 'RPA', '电损电量占比大于等于10%', '', '1', ''])
                        else:
                            audit_list.append([str(bill_number), '电损电量占比', str(proportion_of_power_loss),
                                               str(proportion_of_power_loss), '1', 'RPA', '合格', '', '1', ''])
                    else:
                        auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                        audit_list.append([str(bill_number), '电损电量占比', '0',
                                           str(page_electricity_loss_data), '0', 'RPA', '电损电量占比大于等于10%', '', '1', ''])
                except Exception as e:
                    # print(e)
                    auditResult.auditStatus = '审核中'  # 转人工
                    audit_list.append(
                        [str(bill_number), '电损电量占比', str(float(page_electricity_loss_data) / float(page_share_degrees)),
                         '', '0', 'RPA', 'RPA处理异常', '', '1', ''])
        else:
            if str(page_electricity_loss) == '0':
                audit_list.append(
                    [str(bill_number), '电损电量占比', str(page_electricity_loss), str(page_electricity_loss), '1', 'RPA',
                     '合格', '', '1', ''])
            else:
                auditResult.auditStatus = '审核中'  # 转人工
                audit_list.append([str(bill_number), '电损电量占比', '', '', '0', 'RPA', '电损电量或分摊后度数为空', '', '1', ''])
    else:  # 多个电表
        sum_page_electricity_loss_data = 0.00 # 汇总后的电损
        sum_page_share_degrees = 0.00 # 汇总后的分摊比例
        for data in verification_detail_info_data:
            page_electricity_loss = data['electricity_loss']
            page_share_degrees = data['share_degrees']  # 【分摊后度数(度)】
            if page_electricity_loss and page_share_degrees:
                select_data_list = PyMysql().select_eleinfo1(data)
                page_electricity_loss = select_data_list[2]
                for page_electricity_loss_data in page_electricity_loss:  # 汇总相加后计算
                    sum_page_electricity_loss_data += page_electricity_loss_data
                    sum_page_share_degrees += page_share_degrees
            else:
                if str(page_electricity_loss) == '0':
                    audit_list.append(
                        [str(bill_number), '电损电量占比', '【' + str(data['es_number']) + '】' + str(page_electricity_loss),
                         str(page_electricity_loss), '1', 'RPA', '合格', '', '1', ''])
                else:
                    auditResult.auditStatus = '审核中'  # 转人工
                    audit_list.append(
                        [str(bill_number), '电损电量占比', '【' + str(data['es_number']) + '】' + '', '', '0', 'RPA',
                         '电损电量或分摊后度数为空', '', '1', ''])
        try:
            proportion_of_power_loss = round(
                float(sum_page_electricity_loss_data) / float(sum_page_share_degrees), 2)
            if proportion_of_power_loss >= 0.1:
                auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                audit_list.append([str(bill_number), '电损电量占比',
                                   '【' + str(data['es_number']) + '】' + str(proportion_of_power_loss),
                                   str(proportion_of_power_loss), '0', 'RPA', '电损电量占比大于等于10%',
                                   '', '1', ''])
            else:
                audit_list.append([str(bill_number), '电损电量占比',
                                   '【' + str(data['es_number']) + '】' + str(proportion_of_power_loss),
                                   str(proportion_of_power_loss), '1', 'RPA', '合格', '', '1',
                                   ''])
        except Exception as e:
            print(e)
            auditResult.auditStatus = '审核中'  # 转人工
            audit_list.append(
                [str(bill_number), '电损电量占比',
                 '【' + str(data['es_number']) + '】' + str(
                     float(page_electricity_loss_data) / float(page_share_degrees)),
                 '', '0', 'RPA', 'RPA处理异常', '', '1', ''])
    return auditResult, audit_list


"""
    【稽核规则一】:数据稽核
            规则1：页面【电费核销明细信息】-【电表户号】查找附件二【用户编号】的【客户地址】与对应的页面【报账点名称】作匹对，在明细表识别结果上再加列【报账点名称】
                # ---   不一致：页面【审核备注】：报账点名称与电力清单中名称不一致。
"""
def audit_rule_one(audit_list, bill_number,rar_file,
                   page_rp_name,affix_list_from_db,ocr_uname,file_flag2):
    if '附件2' in rar_file:
        audit_list.append(
            [str(bill_number), '报账点名称', str(page_rp_name), '', '0', 'RPA', '附件2文件格式不正确',
             AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
    else:
        if not file_flag2:
            audit_list.append(
                [str(bill_number), '报账点名称', str(page_rp_name), '', '0', 'RPA', '附件2电力明细清单不存在',
                 AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
        else:
            if str(ocr_uname).replace(' ', ''):
                ocr_uname = str(ocr_uname).replace('（','(').replace('）',')').replace('＃','#')
                page_rp_name = str(page_rp_name).replace('（','(').replace('）',')').replace('＃','#')
                if str(page_rp_name).replace(' ', ''):
                    if str(ocr_uname) != str(page_rp_name):
                        # auditResult.auditStatus = '不通过'  # 返回稽核结果为不通过
                        # audit_refused_type['信息与明细不符'] = 0
                        audit_list.append(
                            [str(bill_number), '报账点名称', str(page_rp_name), str(ocr_uname), '0', 'RPA',
                             '报账点名称与电力清单中名称不一致',
                             AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
                    else:
                        audit_list.append(
                            [str(bill_number), '报账点名称', str(page_rp_name), str(ocr_uname), '1', 'RPA', '合格',
                             AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
                else:
                    # auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                    audit_list.append(
                        [str(bill_number), '报账点名称', '', str(ocr_uname), '0', 'RPA', '页面抓取的报账点名称为空',
                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
            else:
                # auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                audit_list.append([str(bill_number), '报账点名称', str(page_rp_name), '', '0', 'RPA', 'OCR识别的客户地址为空',
                                   AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
    return audit_list

"""
    【稽核规则四】：数据稽核
        规则4：页面【实际总金额】与附件2（以户号为依据）的金额判断，必须完全一致
                # ---   不一致后动作：【审核状态】-【不通过】，备注：【实际报账金额】与【电费明细】不一致

"""
def audit_rule_four(auditResult, audit_list, audit_refused_type,bill_number,whether_execute,rar_file,
                    page_actual_total_amount,affix_list_from_db,file_flag2,ocr_ele_charge,
                    verification_detail_info_data,data,bill_table_data_list):
    """
    :param auditResult:
    :param audit_list:
    :param audit_refused_type:
    :param bill_number:
    :param whether_execute:
    :param rar_file:
    :param page_actual_total_amount: 实际总金额
    :param affix_list_from_db:
    :param file_flag2:
    :param ocr_ele_charge: 电费金额
    :param verification_detail_info_data:
    :param data:
    :param bill_table_data_list:
    :return:
    """
    if whether_execute == False:
        try:
            if '附件2' in rar_file:
                audit_list.append(
                    [str(bill_number), '实际总金额', str(page_actual_total_amount), '', '0', 'RPA', '附件2文件格式不正确',
                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
            else:
                if not file_flag2:
                    audit_list.append(
                        [str(bill_number), '实际总金额', str(page_actual_total_amount), '', '0', 'RPA', '附件2电力明细清单不存在',
                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
                else:
                    if ocr_ele_charge and page_actual_total_amount:
                        if round(float(ocr_ele_charge), 2) != round(float(page_actual_total_amount), 2):
                            auditResult.auditStatus = '不通过'  # 返回稽核结果为不通过
                            audit_refused_type['金额有误'] = 0
                            audit_list.append([str(bill_number), '实际总金额', str(page_actual_total_amount),
                                               str(ocr_ele_charge), '0', 'RPA', '【实际报账金额】与【电费明细】不一致',
                                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '1', ''])
                        else:
                            audit_list.append([str(bill_number), '实际总金额', str(page_actual_total_amount),
                                               str(ocr_ele_charge), '1', 'RPA', '合格',
                                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '1', ''])
                    else:
                        auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                        audit_list.append([str(bill_number), '实际总金额', str(page_actual_total_amount),
                                           str(ocr_ele_charge), '0', 'RPA', '【实际报账金额】为空或【电费明细】为空',
                                           AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '1', ''])
        except Exception as e:
            auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
            audit_list.append([str(bill_number), '实际总金额', '',
                               '', '0', 'RPA', 'RPA处理异常',
                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '1', ''])
    else:
        try:
            if '附件2' in rar_file:
                audit_list.append(
                    [str(bill_number), '实际总金额',
                     '【' + str(data['es_number']) + '】' + str(page_actual_total_amount), '', '0', 'RPA',
                     '附件2文件格式不正确', AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
            else:
                if not file_flag2:
                    audit_list.append(
                        [str(bill_number), '实际总金额',
                         '【' + str(data['es_number']) + '】' + str(page_actual_total_amount), '', '0', 'RPA',
                         '附件2电力明细清单不存在', AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5',
                         ''])
                else:
                    ocr_ele_charge_sum = 0
                    for data in verification_detail_info_data:
                        for bill_table_data in bill_table_data_list:  # 通过户号识别结果列表找到对应的识别内容
                            if str(bill_table_data[0]) == str(data['es_number']):
                                if bill_table_data[1] != '':
                                    ocr_ele_charge = bill_table_data[1]['ele_charge']  # 附件2（以户号为依据）的金额
                                    if ocr_ele_charge != '':
                                        try:
                                            ocr_ele_charge_sum = round(
                                                float(ocr_ele_charge_sum) + float(ocr_ele_charge), 2)
                                        except:
                                            pass
                    if ocr_ele_charge_sum != 0 and page_actual_total_amount:
                        if round(float(ocr_ele_charge_sum), 2) != round(float(page_actual_total_amount), 2):
                            auditResult.auditStatus = '不通过'  # 返回稽核结果为不通过
                            audit_refused_type['金额有误'] = 0
                            audit_list.append(
                                [str(bill_number), '实际总金额', str(page_actual_total_amount), str(ocr_ele_charge_sum), '0',
                                 'RPA', '【实际报账金额】与【电费明细】不一致',
                                 AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '1', ''])
                        else:
                            audit_list.append([str(bill_number), '实际总金额',
                                               str(page_actual_total_amount), str(ocr_ele_charge_sum), '1', 'RPA', '合格',
                                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '1', ''])
                    else:
                        auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                        audit_list.append([str(bill_number), '实际总金额',
                                           '【' + str(data['es_number']) + '】' + str(page_actual_total_amount), '', '0',
                                           'RPA', '【实际报账金额】为空或【电费明细】为空',
                                           AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '1', ''])
        except Exception as e:
            auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
            audit_list.append([str(bill_number), '实际总金额', '', '', '0', 'RPA', 'RPA处理异常',
                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '1', ''])
    return auditResult, audit_list, audit_refused_type

"""
    【稽核规则七】：数据稽核
        规则7：页面【电表核销明细信息】-【上期读数（度）】与附件2根据页面【电表缴费明细信息】-【户号】查找对应的【上期指示】/【上月】等等
                # ---   不一致后动作：【审核状态】-【不通过】，备注：本期读数与电费明细不一致
"""
def audit_rule_lastReding(audit_list, auditResult, audit_refused_type,whether_execute, rar_file, affix_list_from_db, bill_number,
                     file_flag2, page_last_reading, ocr_last_display_num, verification_detail_info_data,
                     bill_table_data_list):
    """

    :param audit_list:
    :param auditResult:
    :param audit_refused_type:
    :param whether_execute:
    :param rar_file:
    :param affix_list_from_db:
    :param bill_number:
    :param file_flag2:
    :param page_current_reading: 页面 - 本期读数（度）
    :param ocr_now_display_num:
    :param verification_detail_info_data:
    :param bill_table_data_list:
    :return:
    """
    if whether_execute == False:
        try:
            if '附件2' in rar_file:
                audit_list.append(
                    [str(bill_number), '上期读数', str(page_last_reading), '', '0', 'RPA', '附件2文件格式不正确',
                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
            else:
                if not file_flag2:
                    audit_list.append(
                        [str(bill_number), '上期读数', str(page_last_reading), '', '0', 'RPA', '附件2电力明细清单不存在',
                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
                else:
                    if ocr_last_display_num and page_last_reading:
                        try:
                            if float(str(page_last_reading).strip()) != float(str(ocr_last_display_num).strip()):
                                auditResult.auditStatus = '不通过'  # 返回稽核结果为不通过
                                audit_refused_type['信息与明细不符'] = 0
                                audit_list.append(
                                    [str(bill_number), '上期读数', str(page_last_reading), str(ocr_last_display_num), '0',
                                     'RPA', '上期读数与电费明细不一致',
                                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
                            else:
                                audit_list.append(
                                    [str(bill_number), '上期读数', str(page_last_reading), str(ocr_last_display_num), '1',
                                     'RPA', '合格',
                                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
                        except Exception as e:
                            # print(e)
                            auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                            audit_list.append(
                                [str(bill_number), '上期读数', str(page_last_reading), str(ocr_last_display_num), '0',
                                 'RPA', 'RPA处理异常',
                                 AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
                    else:
                        auditResult.auditStatus = '审核中'  # 转人工
                        audit_list.append(
                            [str(bill_number), '上期读数', str(page_last_reading), str(ocr_last_display_num), '0', 'RPA',
                             '【上期读数（度）】为空或附件2上期指示为空',
                             AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
        except Exception as e:
            # print(e)
            auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
            audit_list.append(
                [str(bill_number), '上期读数', '', '', '0',
                 'RPA', 'RPA处理异常',
                 AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
    else:
        for data in verification_detail_info_data:
            page_last_reading = data['last_reading']  # 上期读数（度）
            try:
                if '附件2' in rar_file:
                    audit_list.append(
                        [str(bill_number), '上期读数', '【' + str(data['es_number']) + '】' + str(page_last_reading), '',
                         '0', 'RPA', '附件2文件格式不正确',
                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
                else:
                    if not file_flag2:
                        audit_list.append(
                            [str(bill_number), '上期读数', '【' + str(data['es_number']) + '】' + str(page_last_reading),
                             '', '0', 'RPA',
                             '附件2电力明细清单不存在', AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
                    else:
                        ocr_last_display_num = ''
                        for bill_table_data in bill_table_data_list:  # 通过户号识别结果列表找到对应的识别内容
                            if str(bill_table_data[0]) == str(data['es_number']):
                                if bill_table_data[1] != '':
                                    ocr_last_display_num = bill_table_data[1]['last_display_num']  # 【本期指示】/【本月】/本期读数
                        if ocr_last_display_num != '' and page_last_reading:
                            try:
                                if float(str(page_last_reading).strip()) != float(
                                        str(ocr_last_display_num).strip()):
                                    auditResult.auditStatus = '不通过'  # 返回稽核结果为不通过
                                    audit_refused_type['信息与明细不符'] = 0
                                    audit_list.append(
                                        [str(bill_number), '上期读数',
                                         '【' + str(data['es_number']) + '】' + str(page_last_reading),
                                         str(ocr_last_display_num), '0',
                                         'RPA', '上期读数与电费明细不一致',
                                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
                                else:
                                    audit_list.append(
                                        [str(bill_number), '上期读数',
                                         '【' + str(data['es_number']) + '】' + str(page_last_reading),
                                         str(ocr_last_display_num), '1',
                                         'RPA', '合格',
                                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
                            except Exception as e:
                                # print(e)
                                auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                                audit_list.append(
                                    [str(bill_number), '上期读数',
                                     '【' + str(data['es_number']) + '】' + str(page_last_reading),
                                     str(ocr_last_display_num), '0',
                                     'RPA', 'RPA处理异常',
                                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
                        else:
                            auditResult.auditStatus = '审核中'  # 转人工
                            audit_list.append(
                                [str(bill_number), '上期读数',
                                 '【' + str(data['es_number']) + '】' + str(page_last_reading),
                                 str(ocr_last_display_num), '0', 'RPA',
                                 '【上期读数（度）】为空或附件2上期指示为空',
                                 AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
            except Exception as e:
                # print(e)
                auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                audit_list.append(
                    [str(bill_number), '上期读数', '【' + str(data['es_number']) + '】' + '', '', '0',
                     'RPA', 'RPA处理异常',
                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
    return audit_list, auditResult, audit_refused_type


"""
    【稽核规则七】：数据稽核
        规则7：页面【电表核销明细信息】-【本期读数（度）】与附件2根据页面【电表缴费明细信息】-【户号】查找对应的【本期指示】/【本月】等等
                # ---   不一致后动作：【审核状态】-【不通过】，备注：本期读数与电费明细不一致
"""
def audit_rule_seven(audit_list, auditResult, audit_refused_type,whether_execute, rar_file, affix_list_from_db, bill_number,
                     file_flag2, page_current_reading, ocr_now_display_num, verification_detail_info_data,
                     bill_table_data_list):
    """

    :param audit_list:
    :param auditResult:
    :param audit_refused_type:
    :param whether_execute:
    :param rar_file:
    :param affix_list_from_db:
    :param bill_number:
    :param file_flag2:
    :param page_current_reading: 页面 - 本期读数（度）
    :param ocr_now_display_num:
    :param verification_detail_info_data:
    :param bill_table_data_list:
    :return:
    """
    if whether_execute == False:
        try:
            if '附件2' in rar_file:
                audit_list.append(
                    [str(bill_number), '本期读数', str(page_current_reading), '', '0', 'RPA', '附件2文件格式不正确',
                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
            else:
                if not file_flag2:
                    audit_list.append(
                        [str(bill_number), '本期读数', str(page_current_reading), '', '0', 'RPA', '附件2电力明细清单不存在',
                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
                else:
                    if ocr_now_display_num and page_current_reading:
                        try:
                            if float(str(page_current_reading).strip()) != float(str(ocr_now_display_num).strip()):
                                auditResult.auditStatus = '不通过'  # 返回稽核结果为不通过
                                audit_refused_type['信息与明细不符'] = 0
                                audit_list.append(
                                    [str(bill_number), '本期读数', str(page_current_reading), str(ocr_now_display_num), '0',
                                     'RPA', '本期读数与电费明细不一致',
                                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
                            else:
                                audit_list.append(
                                    [str(bill_number), '本期读数', str(page_current_reading), str(ocr_now_display_num), '1',
                                     'RPA', '合格',
                                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
                        except Exception as e:
                            # print(e)
                            auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                            audit_list.append(
                                [str(bill_number), '本期读数', str(page_current_reading), str(ocr_now_display_num), '0',
                                 'RPA', 'RPA处理异常',
                                 AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
                    else:
                        auditResult.auditStatus = '审核中'  # 转人工
                        audit_list.append(
                            [str(bill_number), '本期读数', str(page_current_reading), str(ocr_now_display_num), '0', 'RPA',
                             '【本期读数（度）】为空或附件2本期指示为空',
                             AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
        except Exception as e:
            # print(e)
            auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
            audit_list.append(
                [str(bill_number), '本期读数', '', '', '0',
                 'RPA', 'RPA处理异常',
                 AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
    else:
        for data in verification_detail_info_data:
            page_current_reading = data['current_reading']  # 本期读数（度）
            try:
                if '附件2' in rar_file:
                    audit_list.append(
                        [str(bill_number), '本期读数', '【' + str(data['es_number']) + '】' + str(page_current_reading), '',
                         '0', 'RPA', '附件2文件格式不正确',
                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
                else:
                    if not file_flag2:
                        audit_list.append(
                            [str(bill_number), '本期读数', '【' + str(data['es_number']) + '】' + str(page_current_reading),
                             '', '0', 'RPA',
                             '附件2电力明细清单不存在', AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
                    else:
                        ocr_now_display_num = ''
                        for bill_table_data in bill_table_data_list:  # 通过户号识别结果列表找到对应的识别内容
                            if str(bill_table_data[0]) == str(data['es_number']):
                                if bill_table_data[1] != '':
                                    ocr_now_display_num = bill_table_data[1]['now_display_num']  # 【本期指示】/【本月】/本期读数
                        if ocr_now_display_num != '' and page_current_reading:
                            try:
                                if float(str(page_current_reading).strip()) != float(
                                        str(ocr_now_display_num).strip()):
                                    auditResult.auditStatus = '不通过'  # 返回稽核结果为不通过
                                    audit_refused_type['信息与明细不符'] = 0
                                    audit_list.append(
                                        [str(bill_number), '本期读数',
                                         '【' + str(data['es_number']) + '】' + str(page_current_reading),
                                         str(ocr_now_display_num), '0',
                                         'RPA', '本期读数与电费明细不一致',
                                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
                                else:
                                    audit_list.append(
                                        [str(bill_number), '本期读数',
                                         '【' + str(data['es_number']) + '】' + str(page_current_reading),
                                         str(ocr_now_display_num), '1',
                                         'RPA', '合格',
                                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
                            except Exception as e:
                                # print(e)
                                auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                                audit_list.append(
                                    [str(bill_number), '本期读数',
                                     '【' + str(data['es_number']) + '】' + str(page_current_reading),
                                     str(ocr_now_display_num), '0',
                                     'RPA', 'RPA处理异常',
                                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
                        else:
                            auditResult.auditStatus = '审核中'  # 转人工
                            audit_list.append(
                                [str(bill_number), '本期读数',
                                 '【' + str(data['es_number']) + '】' + str(page_current_reading),
                                 str(ocr_now_display_num), '0', 'RPA',
                                 '【本期读数（度）】为空或附件2本期指示为空',
                                 AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
            except Exception as e:
                # print(e)
                auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                audit_list.append(
                    [str(bill_number), '本期读数', '【' + str(data['es_number']) + '】' + '', '', '0',
                     'RPA', 'RPA处理异常',
                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
    return audit_list, auditResult, audit_refused_type

"""
    稽核规则八：【数据稽核】
       规则8：页面【电表核销明细信息】-【电费单价（元/度）】与附件2根据页面【电表缴费明细信息】-【户号】查找对应的【电价】/【电费信息】之和等等
                # ---   不一致后动作：【审核状态】-【不通过】，备注：电费单价与电费明细不一致 
"""
def audit_rule_eight(auditResult, audit_list, audit_refused_type, whether_execute, rar_file, bill_number, file_flag2,
                     verification_detail_info_data, page_electricity_price,ocr_ele_price,affix_list_from_db,bill_table_data_list):
    if whether_execute == False:
        try:
            if '附件2' in rar_file:
                audit_list.append(
                    [str(bill_number), '电费单价', str(page_electricity_price), '', '0', 'RPA', '附件2文件格式不正确',
                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
            else:
                if not file_flag2:
                    audit_list.append(
                        [str(bill_number), '电费单价', str(page_electricity_price), '', '0', 'RPA', '附件2电力明细清单不存在',
                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
                else:
                    if page_electricity_price and ocr_ele_price:
                        for page_electricity_price_data in page_electricity_price:
                            try:
                                for price_sum, ocr_ele_price_value in enumerate(ocr_ele_price.split(';')):
                                    if round(float(ocr_ele_price_value), 4) != round(
                                            float(str(page_electricity_price_data).strip()), 4):
                                        if price_sum == len(ocr_ele_price.split(';')) - 1:
                                            auditResult.auditStatus = '不通过'  # 返回稽核结果为不通过
                                            audit_refused_type['信息与明细不符'] = 0
                                            audit_list.append(
                                                [str(bill_number), '电费单价', str(page_electricity_price_data),
                                                 str(ocr_ele_price), '0',
                                                 'RPA', '电费单价与电费明细不一致',
                                                 AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4',
                                                 ''])
                                    else:
                                        audit_list.append(
                                            [str(bill_number), '电费单价', str(page_electricity_price_data),
                                             str(ocr_ele_price_value), '1',
                                             'RPA', '合格',
                                             AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
                                        break
                            except Exception as e:
                                # print(e)
                                auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                                audit_list.append(
                                    [str(bill_number), '电费单价', str(page_electricity_price_data), str(ocr_ele_price),
                                     '0', 'RPA',
                                     'RPA处理异常',
                                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
                    else:
                        auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                        audit_list.append(
                            [str(bill_number), '电费单价', str(page_electricity_price), str(ocr_ele_price), '0', 'RPA',
                             '【电费单价（元/度）】为空或附件2电价为空',
                             AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
        except Exception as e:
            # print(e)
            auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
            audit_list.append(
                [str(bill_number), '电费单价', '', '', '0', 'RPA',
                 'RPA处理异常',
                 AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
    else:
        for data in verification_detail_info_data:
            try:
                if '附件2' in rar_file:
                    audit_list.append(
                        [str(bill_number), '电费单价', '【' + str(data['es_number']) + '】' + str(page_electricity_price), '',
                         '0', 'RPA',
                         '附件2文件格式不正确', AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
                else:
                    if not file_flag2:
                        audit_list.append(
                            [str(bill_number), '电费单价', '【' + str(data['es_number']) + '】' + str(page_electricity_price),
                             '', '0', 'RPA',
                             '附件2电力明细清单不存在', AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
                    else:
                        page_electricity_price = data['electricity_price']
                        ocr_ele_price = ''
                        for bill_table_data in bill_table_data_list:  # 通过户号识别结果列表找到对应的识别内容
                            if str(bill_table_data[0]) == str(data['es_number']):
                                if bill_table_data[1] != '':
                                    ocr_ele_price = bill_table_data[1]['ele_price']  # 【电价】/【电费信息】
                        if page_electricity_price and ocr_ele_price != '':
                            select_data_list = PyMysql().select_eleinfo1(data)
                            page_electricity_price = select_data_list[0]
                            for page_electricity_price_data in page_electricity_price:
                                try:
                                    for price_sum, ocr_ele_price_value in enumerate(ocr_ele_price.split(';')):
                                        if round(float(ocr_ele_price_value), 4) != round(
                                                float(str(page_electricity_price_data).strip()), 4):
                                            if price_sum == len(ocr_ele_price.split(';')) - 1:
                                                auditResult.auditStatus = '不通过'  # 返回稽核结果为不通过
                                                audit_refused_type['信息与明细不符'] = 0
                                                audit_list.append(
                                                    [str(bill_number), '电费单价', '【' + str(data['es_number']) + '】' + str(
                                                        page_electricity_price_data),
                                                     str(ocr_ele_price), '0',
                                                     'RPA', '电费单价与电费明细不一致',
                                                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4',
                                                     ''])
                                        else:
                                            audit_list.append(
                                                [str(bill_number), '电费单价',
                                                 '【' + str(data['es_number']) + '】' + str(page_electricity_price_data),
                                                 str(ocr_ele_price_value), '1',
                                                 'RPA', '合格',
                                                 AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4',
                                                 ''])
                                            break
                                except Exception as e:
                                    # print(e)
                                    auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                                    audit_list.append(
                                        [str(bill_number), '电费单价',
                                         '【' + str(data['es_number']) + '】' + str(page_electricity_price_data),
                                         str(ocr_ele_price), '0', 'RPA',
                                         'RPA处理异常',
                                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
                        else:
                            auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                            audit_list.append(
                                [str(bill_number), '电费单价',
                                 '【' + str(data['es_number']) + '】' + str(page_electricity_price), str(ocr_ele_price),
                                 '0', 'RPA',
                                 '【电费单价（元/度）】为空或附件2电价为空',
                                 AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
            except Exception as e:
                # print(e)
                auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                audit_list.append(
                    [str(bill_number), '电费单价', '【' + str(data['es_number']) + '】' + '', '', '0', 'RPA',
                     'RPA处理异常',
                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
    return auditResult, audit_list, audit_refused_type

"""
    稽核规则九：【数据稽核】
        规则9：如果【本期读数】判断通过：页面【电表缴费明细信息】-【用电量（度）】=【本期读数（度）】减去【上期读数（度）】，此条规则需完全一致。
                # ---   如果附件2无【本期读数】：页面【电表缴费明细信息】-【用电量（度）】+（【电损电量】/【本次实际分摊比例】）=附件2根据页面【电表缴费明细信息】-【户号】查找对应的【总结算电量】，
                # ---   或者如果附件2无【本期读数】：页面【电表缴费明细信息】-【用电量（度）】=附件2根据页面【电表缴费明细信息】-【户号】查找对应的【电量】。具体是那种情况，需在票据学习时区分附件2中电量是否包括电损。
                # ---   后面2条规则四舍五入匹配，相差±1度均可判断通过。
                # ---   不一致后动作：【审核状态】-【不通过】，备注：用电量与电费明细不一致
                # ---   备注：如果页面【电表核销明细信息】-【电费单价（元/度）】存在多条只需核对用电量非0的，核对单据存在一对一或多对一情况。
"""
def audir_rule_nine(audit_list, auditResult, audit_refused_type, rar_file,  whether_execute, bill_number,page_electricity_loss,
                    affix_list_from_db,page_current_reading,page_last_reading,
                    verification_detail_info_data, file_flag2, page_electricity_consumption, ocr_last_display_num, ocr_now_display_num,
                    ocr_ele_loss,es_magnification,page_current_share_ratio,ocr_total_power,bill_table_data_list):
    """
    :param audit_list:
    :param auditResult:
    :param audit_refused_type:
    :param rar_file:
    :param whether_execute:
    :param bill_number:
    :param affix_list_from_db:
    :param page_current_reading:
    :param page_last_reading:
    :param verification_detail_info_data:
    :param file_flag2:
    :param page_electricity_consumption: 用电量（度）
    :param ocr_last_display_num: 上期读数
    :param ocr_now_display_num: 【本期指示】/【本月】/本期读数
    :param ocr_ele_loss:
    :param es_magnification:
    :param page_current_share_ratio:
    :param ocr_total_power:
    :param bill_table_data_list:
    :return:
    """
    if whether_execute == False:
        try:
            if '附件2' in rar_file:
                audit_list.append(
                    [str(bill_number), '用电量', str(page_electricity_consumption), '', '0', 'RPA', '附件2文件格式不正确',
                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
            else:
                if not file_flag2:
                    audit_list.append(
                        [str(bill_number), '用电量', str(page_electricity_consumption), '', '0', 'RPA', '附件2电力明细清单不存在',
                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
                else:
                    if ocr_now_display_num and ocr_last_display_num :
                        audit_list.append([str(bill_number), '用电量', str(page_electricity_consumption),
                                                           str(page_electricity_consumption),
                                                           '1', 'RPA', '合格',
                                                           AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'),
                                                           '4', ''])
                    else:
                        n_1 = (round(float(sum(page_electricity_consumption)), 2) + round(float(sum(page_electricity_loss)), 2))/ (page_current_share_ratio /100)
                        if ocr_total_power == n_1:
                            audit_list.append([str(bill_number), '用电量', str(n_1),
                                               str(ocr_total_power),
                                               '1', 'RPA', '合格',
                                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'),
                                               '4', ''])
                        else:
                            auditResult.auditStatus = '不通过'  # 返回稽核结果为不通过
                            audit_refused_type['信息与明细不符'] = 0
                            audit_list.append(
                                [str(bill_number), '用电量', str(n_1),
                                 str(ocr_total_power), '0', 'RPA', '用电量与电费明细不一致',
                                 AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])

        except Exception as e:
            # print(e)
            auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
            audit_list.append([str(bill_number), '用电量', '',
                               '',
                               '0', 'RPA', 'RPA处理异常', AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'),
                               '4', ''])
    else:
        for data in verification_detail_info_data:
            try:
                if '附件2' in rar_file:
                    audit_list.append(
                        [str(bill_number), '用电量',
                         '【' + str(data['es_number']) + '】' + str(page_electricity_consumption), '', '0', 'RPA',
                         '附件2文件格式不正确', AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
                else:
                    if not file_flag2:
                        audit_list.append(
                            [str(bill_number), '用电量',
                             '【' + str(data['es_number']) + '】' + str(page_electricity_consumption), '', '0', 'RPA',
                             '附件2电力明细清单不存在', AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
                    else:
                        select_data_list = PyMysql().select_eleinfo1(data)
                        page_electricity_consumption = select_data_list[1]
                        page_electricity_loss = select_data_list[2]
                        ocr_now_display_num = ''
                        ocr_last_display_num = ''
                        for bill_table_data in bill_table_data_list:  # 通过户号识别结果列表找到对应的识别内容
                            if str(bill_table_data[0]) == str(data['es_number']):
                                if bill_table_data[1] != '':
                                    ocr_now_display_num = bill_table_data[1]['now_display_num']  # 【本期指示】/【本月】/本期读数
                                    if ocr_now_display_num:
                                        ocr_now_display_num = round(float(ocr_now_display_num.replace(' ', '')), 2)
                                    else:
                                        ocr_now_display_num = 0
                                    ocr_last_display_num = bill_table_data[1]['last_display_num']  # 上期读数
                                    ocr_total_power = bill_table_data[1]['total_power']
                        # if page_electricity_consumption and ocr_now_display_num != '' and ocr_last_display_num != '':
                        #     for page_electricity_consumption_data in page_electricity_consumption:
                        #         try:
                        #             if abs(round(float(page_electricity_consumption_data), 2) - (round(
                        #                     float(ocr_now_display_num) - float(
                        #                         ocr_last_display_num), 2)) * int(es_magnification)) > 1:
                        #                 auditResult.auditStatus = '不通过'  # 返回稽核结果为不通过
                        #                 audit_refused_type['信息与明细不符'] = 0
                        #                 audit_list.append([str(bill_number), '用电量',
                        #                                    '【' + str(data['es_number']) + '】' + str(
                        #                                        page_electricity_consumption_data),
                        #                                    str(float(ocr_now_display_num) - float(
                        #                                        ocr_last_display_num)),
                        #                                    '0', 'RPA', '【用电量（度）】=【本期读数（度）】减去【上期读数（度）】判断不通过',
                        #                                    AffixUtils.get_affix_url_with_name(affix_list_from_db,
                        #                                                                       '附件2'), '4', ''])
                        #             else:
                        #                 audit_list.append([str(bill_number), '用电量',
                        #                                    '【' + str(data['es_number']) + '】' + str(
                        #                                        page_electricity_consumption_data),
                        #                                    str((float(ocr_now_display_num) - float(
                        #                                        ocr_last_display_num)) * int(es_magnification)),
                        #                                    '1', 'RPA', '合格',
                        #                                    AffixUtils.get_affix_url_with_name(affix_list_from_db,
                        #                                                                       '附件2'),
                        #                                    '4', ''])
                        #         except Exception as e:
                        #             # print(e)
                        #             auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                        #             audit_list.append(
                        #                 [str(bill_number), '用电量',
                        #                  '【' + str(data['es_number']) + '】' + str(page_electricity_consumption_data),
                        #                  str(float(ocr_now_display_num) - float(ocr_last_display_num)),
                        #                  '0', 'RPA', 'RPA处理异常',
                        #                  AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
                        # elif ocr_now_display_num != '':
                        #     # 判断附件2是否有电损
                        #     for page_electricity_consumption_data in page_electricity_consumption:
                        #         try:
                        #             if ocr_ele_loss and float(ocr_ele_loss) > 0:
                        #                 n_1 = round(float((ocr_ele_loss)) / float(
                        #                     page_current_share_ratio) / 100 + float(
                        #                     page_electricity_consumption_data), 2)
                        #                 if abs(n_1 - float(ocr_total_power)) <= 1:
                        #                     # 有电损通过
                        #                     audit_list.append(
                        #                         [str(bill_number), '用电量', '【' + str(data['es_number']) + '】' + str(n_1),
                        #                          str(ocr_total_power), '1', 'RPA', '合格',
                        #                          AffixUtils.get_affix_url_with_name(affix_list_from_db,
                        #                                                             '附件2'), '4', ''])
                        #                 else:
                        #                     # 有电损不通过
                        #                     auditResult.auditStatus = '不通过'  # 返回稽核结果为不通过
                        #                     audit_refused_type['信息与明细不符'] = 0
                        #                     audit_list.append(
                        #                         [str(bill_number), '用电量', '【' + str(data['es_number']) + '】' + str(n_1),
                        #                          str(ocr_total_power), '0', 'RPA',
                        #                          '用电量与电费明细不一致',
                        #                          AffixUtils.get_affix_url_with_name(affix_list_from_db,
                        #                                                             '附件2'), '4', ''])
                        #             else:
                        #                 if float(ocr_total_power) != float(
                        #                         page_electricity_consumption_data):
                        #                     # 无电损不通过
                        #                     auditResult.auditStatus = '不通过'  # 返回稽核结果为不通过
                        #                     audit_refused_type['信息与明细不符'] = 0
                        #                     audit_list.append([str(bill_number), '用电量',
                        #                                        '【' + str(data['es_number']) + '】' + str(
                        #                                            page_electricity_consumption_data),
                        #                                        str(ocr_total_power), '0', 'RPA',
                        #                                        '用电量与电费明细不一致',
                        #                                        AffixUtils.get_affix_url_with_name(affix_list_from_db,
                        #                                                                           '附件2'), '4', ''])
                        #                 else:
                        #                     audit_list.append([str(bill_number), '用电量',
                        #                                        '【' + str(data['es_number']) + '】' + str(
                        #                                            page_electricity_consumption_data),
                        #                                        str(ocr_total_power), '1', 'RPA', '合格',
                        #                                        AffixUtils.get_affix_url_with_name(affix_list_from_db,
                        #                                                                           '附件2'), '4', ''])
                        #         except Exception as e:
                        #             # print(e)
                        #             auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                        #             audit_list.append(
                        #                 [str(bill_number), '用电量',
                        #                  '【' + str(data['es_number']) + '】' + str(page_electricity_consumption_data),
                        #                  str(ocr_total_power), '0',
                        #                  'RPA', 'RPA处理异常',
                        #                  AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
                        # else:
                        #     auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                        #     audit_list.append([str(bill_number), '用电量',
                        #                        '【' + str(data['es_number']) + '】' + str(
                        #                            page_electricity_consumption) + ',' + str(
                        #                            page_last_reading) + ',' + str(
                        #                            page_current_reading),
                        #                        '', '0', 'RPA', '【用电量（度）】为空或【本期读数（度）】为空或【上期读数（度）】为空',
                        #                        AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
                        if ocr_now_display_num and ocr_last_display_num:
                            audit_list.append([str(bill_number), '用电量', str(page_electricity_consumption),
                                               str(page_electricity_consumption),
                                               '1', 'RPA', '合格',
                                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'),
                                               '4', ''])
                        else:
                            n_1 = (round(float(sum(page_electricity_consumption)), 2) + round(
                                float(sum(page_electricity_loss)), 2)) / (page_current_share_ratio /100)
                            if ocr_total_power == n_1:
                                audit_list.append([str(bill_number), '用电量', str(n_1),
                                                   str(ocr_total_power),
                                                   '1', 'RPA', '合格',
                                                   AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'),
                                                   '4', ''])
                            else:
                                auditResult.auditStatus = '不通过'  # 返回稽核结果为不通过
                                audit_refused_type['信息与明细不符'] = 0
                                audit_list.append(
                                    [str(bill_number), '用电量', str(n_1),
                                     str(ocr_total_power), '0', 'RPA', '用电量与电费明细不一致',
                                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])

            except Exception as e:
                # print(e)
                auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                audit_list.append([str(bill_number), '用电量', '【' + str(data['es_number']) + '】' + '',
                                   '',
                                   '0', 'RPA', 'RPA处理异常',
                                   AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
    return audit_list, auditResult, audit_refused_type

"""
    稽核规则十：【数据稽核】
        规则10：前提：本次分摊比例稽核正确：页面【电表核销明细信息】-【电损电量（度）】与附件2（有单独列出电损电量）：页面【电表核销明细信息】-【电损电量（度）】=【本次实际分摊比例】*附件2的【电损值】；
                # ---    如附件2中同时有总电量（包括电损）、上期读数和本期读数，则页面【电表核销明细信息】-【电损电量（度）】=【本次实际分摊比例】*（附件2的【总电量】-附件2【本期读数-上期读数】）。
                # ---    如果附件2（不符合上面情况或无单独列出电损电量）直接跳过。具体是那种情况，需在票据学习时区分附件2中总电量是否包括电损。四舍五入匹配，相差±1度均可判断通过。
                # ---    不一致后动作：【审核状态】-【不通过】，备注：电损电量与电费明细不一致 
"""
def audit_rule_ten(auditResult, audit_list, whether_execute, rar_file, bill_number, affix_list_from_db,
                   audit_refused_type,result,ocr_total_power,ocr_last_display_num,es_magnification,
                   file_flag2, verification_detail_info_data, page_electricity_loss, ocr_ele_loss,
                   page_current_share_ratio, ocr_now_display_num, bill_table_data_list):
    """

    :param auditResult:
    :param audit_list:
    :param whether_execute:
    :param rar_file:
    :param bill_number:
    :param affix_list_from_db:
    :param audit_refused_type:
    :param result:
    :param ocr_total_power:
    :param ocr_last_display_num:
    :param file_flag2:
    :param verification_detail_info_data:
    :param page_electricity_loss:
    :param ocr_ele_loss:
    :param page_current_share_ratio:
    :param ocr_now_display_num:
    :param bill_table_data_list:
    :return:
    """
    if whether_execute == False:
        try:
            if '附件2' in rar_file:
                audit_list.append(
                    [str(bill_number), '电损电量', page_electricity_loss, '', '0', 'RPA', '附件2文件格式不正确',
                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
            else:
                if not file_flag2:
                    audit_list.append(
                        [str(bill_number), '电损电量', page_electricity_loss, '', '0', 'RPA', '附件2电力明细清单不存在',
                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
                else:
                    """
                        附件2中同时有总电量（包括电损）、上期读数和本期读数，
                        则页面【电表缴费明细信息】-【电损电量（度）】=【本次实际分摊比例】*（附件2的【总电量】-附件2【本期读数-上期读数】）* 电表倍率
                    """
                    if isinstance(page_electricity_loss, list):
                        page_electricity_loss = ''.join(page_electricity_loss)
                    if ocr_now_display_num and ocr_last_display_num and ocr_total_power:
                        co_electricity_loss = (float(page_current_share_ratio)/100 ) * \
                                              (float(ocr_total_power) - (float(ocr_now_display_num) - float(ocr_last_display_num))) \
                                              * float(es_magnification)

                        if co_electricity_loss == float(page_electricity_loss):

                            audit_list.append([str(bill_number), '电损电量', str(page_electricity_loss), str(co_electricity_loss),
                                                       '1',
                                                       'RPA', '合格',
                                                       AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
                        else:
                            auditResult.auditStatus = '不通过'  # 返回稽核结果为不通过
                            audit_refused_type['信息与明细不符'] = 0
                            audit_list.append([str(bill_number), '电损电量', str(page_electricity_loss), str(co_electricity_loss),
                                               '0',
                                               'RPA', '电损电量与电费明细不一致',
                                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
                    else:
                        select_data_list = PyMysql().select_eleinfo('verification_detail_info', 'verification_code', bill_number)
                        if not ocr_ele_loss:
                            for select_data in select_data_list[2]:
                                if float(select_data) != 0:
                                    auditResult.auditStatus = '审核中'  # 转人工
                                    audit_list.append(
                                        [str(bill_number), '电损电量', str(ocr_ele_loss), '', '0', 'RPA',
                                         '附件2的【电损值】OCR识别为空',
                                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'),
                                         '4', ''])
                                else:
                                    audit_list.append([str(bill_number), '电损电量', str(select_data),
                                                       '0', '1', 'RPA', '合格',
                                                       AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'),
                                                       '4', ''])
                                    break
                        else:
                            for sum, select_data in enumerate(select_data_list[2]):
                                if sum >= 1 and float(select_data) == 0:
                                    continue

                                temp_ele_loss = abs(float(select_data) - (float(page_current_share_ratio) / 100) * float(
                                    ocr_ele_loss) * 0.01)
                                if temp_ele_loss > 1:
                                    auditResult.auditStatus = '不通过'  # 返回稽核结果为不通过
                                    audit_refused_type['信息与明细不符'] = 0
                                    audit_list.append([str(bill_number), '电损电量', str(select_data),
                                                       str(temp_ele_loss), '0', 'RPA',
                                                       '电损电量与电费明细不一致',
                                                       AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4',
                                                       ''])
                                else:
                                    audit_list.append([str(bill_number), '电损电量', str(select_data),
                                                       str(temp_ele_loss), '1', 'RPA', '合格',
                                                       AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'),
                                                       '4', ''])
        except Exception as e:
            # print(e)
            result['success'] = 1  # 返回稽核结果为转人工
            audit_list.append([str(bill_number), '电损电量','','',
                               '0', 'RPA', 'RPA处理异常', AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'),
                               '4', ''])
    else:
        for data in verification_detail_info_data:
            try:
                if '附件2' in rar_file:
                    audit_list.append(
                        [str(bill_number), '电损电量', '【' + str(data['es_number']) + '】' + page_electricity_loss, '', '0',
                         'RPA', '附件2文件格式不正确',
                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
                else:
                    if not file_flag2:
                        audit_list.append(
                            [str(bill_number), '电损电量', '【' + str(data['es_number']) + '】' + page_electricity_loss, '',
                             '0', 'RPA',
                             '附件2电力明细清单不存在', AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
                    else:
                        # try:
                        ocr_ele_loss = ''
                        select_data_list = PyMysql().select_eleinfo1(data)
                        page_electricity_loss = select_data_list[2]
                        page_current_share_ratio = data['current_share_ratio']  # 本次实际分摊比例
                        page_current_share_ratio = \
                        re.findall(r"\d+\.?\d*", str(page_current_share_ratio).replace('%', ''))[0]
                        ocr_now_display_num = ''
                        ocr_last_display_num = ''
                        for bill_table_data in bill_table_data_list:  # 通过户号识别结果列表找到对应的识别内容
                            if str(bill_table_data[0]) == str(data['es_number']):
                                if bill_table_data[1] != '':
                                    ocr_ele_loss = bill_table_data[1]['ele_loss']  # 电损值
                                    ocr_now_display_num = bill_table_data[1]['now_display_num']  # 【本期指示】/【本月】/本期读数
                                    if ocr_now_display_num:
                                        ocr_now_display_num = round(float(ocr_now_display_num.replace(' ', '')), 2)
                                    else:
                                        ocr_now_display_num = 0
                                    ocr_last_display_num = bill_table_data[1]['last_display_num']  # 上期读数
                        """
                            附件2中同时有总电量（包括电损）、上期读数和本期读数，
                            则页面【电表缴费明细信息】-【电损电量（度）】=【本次实际分摊比例】*（附件2的【总电量】-附件2【本期读数-上期读数】）* 电表倍率
                        """
                        if ocr_now_display_num and ocr_last_display_num and ocr_total_power:
                            co_electricity_loss = (float(page_current_share_ratio) / 100) * \
                                                  (float(ocr_total_power) - (
                                                              float(ocr_now_display_num) - float(ocr_last_display_num))) \
                                                  * float(es_magnification)
                            if co_electricity_loss == float(page_electricity_loss):

                                audit_list.append(
                                    [str(bill_number), '电损电量', str(page_electricity_loss), str(co_electricity_loss),
                                     '1',
                                     'RPA', '合格',
                                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
                            else:
                                auditResult.auditStatus = '不通过'  # 返回稽核结果为不通过
                                audit_refused_type['信息与明细不符'] = 0
                                audit_list.append(
                                    [str(bill_number), '电损电量', str(page_electricity_loss), str(co_electricity_loss),
                                     '0',
                                     'RPA', '电损电量与电费明细不一致',
                                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
                        else:

                            if not ocr_ele_loss:
                                for select_data in select_data_list[2]:
                                    if float(select_data) != 0:
                                        auditResult.auditStatus = '审核中'  # 转人工
                                        audit_list.append(
                                            [str(bill_number), '电损电量', str(ocr_ele_loss), '', '0', 'RPA',
                                             '附件2的【电损值】OCR识别为空',
                                             AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'),
                                             '4', ''])
                                    else:
                                        audit_list.append([str(bill_number), '电损电量', str(select_data),
                                                           '0', '1', 'RPA', '合格',
                                                           AffixUtils.get_affix_url_with_name(affix_list_from_db,
                                                                                              '附件2'),
                                                           '4', ''])
                                        break
                            else:
                                for sum, select_data in enumerate(select_data_list[2]):
                                    if sum >= 1 and float(select_data) == 0:
                                        continue

                                    temp_ele_loss = abs(
                                        float(select_data) - (float(page_current_share_ratio) / 100) * float(
                                            ocr_ele_loss) * 0.01)
                                    if temp_ele_loss > 1:
                                        auditResult.auditStatus = '不通过'  # 返回稽核结果为不通过
                                        audit_refused_type['信息与明细不符'] = 0
                                        audit_list.append([str(bill_number), '电损电量', str(select_data),
                                                           str(temp_ele_loss), '0', 'RPA',
                                                           '电损电量与电费明细不一致',
                                                           AffixUtils.get_affix_url_with_name(affix_list_from_db,
                                                                                              '附件2'), '4',
                                                           ''])
                                    else:
                                        audit_list.append([str(bill_number), '电损电量', str(select_data),
                                                           str(temp_ele_loss), '1', 'RPA', '合格',
                                                           AffixUtils.get_affix_url_with_name(affix_list_from_db,
                                                                                              '附件2'),
                                                           '4', ''])
            except Exception as e:
                # print(e)
                result['success'] = 1  # 返回稽核结果为转人工
                audit_list.append([str(bill_number), '电损电量', '【' + str(data['es_number']) + '】' + '','',
                                   '0', 'RPA', 'RPA处理异常',
                                   AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'),
                                   '4', ''])
    return auditResult, audit_list,audit_refused_type,result

# def audit_rule_ten(auditResult, audit_list, whether_execute, rar_file, bill_number, affix_list_from_db,
#                    audit_refused_type,result,ocr_total_power,ocr_last_display_num,
#                    file_flag2, verification_detail_info_data, page_electricity_loss, ocr_ele_loss,
#                    page_current_share_ratio, ocr_now_display_num, bill_table_data_list):
#     if whether_execute == False:
#         try:
#             if '附件2' in rar_file:
#                 audit_list.append(
#                     [str(bill_number), '电损电量', page_electricity_loss, '', '0', 'RPA', '附件2文件格式不正确',
#                      AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
#             else:
#                 if not file_flag2:
#                     audit_list.append(
#                         [str(bill_number), '电损电量', page_electricity_loss, '', '0', 'RPA', '附件2电力明细清单不存在',
#                          AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
#                 else:
#                     try:
#                         if not ocr_ele_loss:
#                             if str(page_electricity_loss).replace("[", "").replace("]", "").replace("'", "") == '0':
#                                 audit_list.append(
#                                     [str(bill_number), '电损电量', str(page_electricity_loss), str(page_electricity_loss),
#                                      '1', 'RPA', '合格', AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'),
#                                      '4', ''])
#                             else:
#                                 auditResult.auditStatus = '审核中'  # 转人工
#                                 audit_list.append(
#                                     [str(bill_number), '电损电量', page_electricity_loss, str(ocr_ele_loss), '0', 'RPA',
#                                      '附件2的【电损值】OCR识别为空',
#                                      AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
#                         elif round(float(ocr_ele_loss), 2) != round(
#                                 float(str(page_current_share_ratio).replace('%', '')) * float(
#                                         ocr_ele_loss) * 0.01, 2):
#                             auditResult.auditStatus = '不通过'  # 返回稽核结果为不通过
#                             audit_refused_type['信息与明细不符'] = 0
#                             audit_list.append([str(bill_number), '电损电量', str(ocr_ele_loss), str(
#                                 float(str(page_current_share_ratio).replace('%', '')) * float(ocr_ele_loss) * 0.01),
#                                                '0',
#                                                'RPA', '电损电量与电费明细不一致',
#                                                AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
#                         else:
#                             audit_list.append([str(bill_number), '电损电量', str(page_electricity_loss), str(
#                                 float(str(page_current_share_ratio).replace('%', '')) * float(ocr_ele_loss) * 0.01),
#                                                '1',
#                                                'RPA', '合格',
#                                                AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
#                     except Exception as e:
#                         # print(e)
#                         result['success'] = 1  # 返回稽核结果为转人工
#                         audit_list.append([str(bill_number), '电损电量', str(page_electricity_loss),
#                                            str(page_current_share_ratio) + '% *(' + str(ocr_total_power) + '-' + str(
#                                                ocr_now_display_num) + '-' + str(ocr_last_display_num) + ')',
#                                            '0', 'RPA', 'RPA处理异常',
#                                            AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'),
#                                            '4', ''])
#         except Exception as e:
#             # print(e)
#             result['success'] = 1  # 返回稽核结果为转人工
#             audit_list.append([str(bill_number), '电损电量', '',
#                                '0', 'RPA', 'RPA处理异常', AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'),
#                                '4', ''])
#     else:
#         for data in verification_detail_info_data:
#             try:
#                 if '附件2' in rar_file:
#                     audit_list.append(
#                         [str(bill_number), '电损电量', '【' + str(data['es_number']) + '】' + page_electricity_loss, '', '0',
#                          'RPA', '附件2文件格式不正确',
#                          AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
#                 else:
#                     if not file_flag2:
#                         audit_list.append(
#                             [str(bill_number), '电损电量', '【' + str(data['es_number']) + '】' + page_electricity_loss, '',
#                              '0', 'RPA',
#                              '附件2电力明细清单不存在', AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
#                     else:
#                         try:
#                             ocr_ele_loss = ''
#                             select_data_list = PyMysql().select_eleinfo1(data)
#                             page_electricity_loss = select_data_list[2]
#                             page_current_share_ratio = data['current_share_ratio']  # 本次实际分摊比例
#                             page_current_share_ratio = \
#                             re.findall(r"\d+\.?\d*", str(page_current_share_ratio).replace('%', ''))[0]
#                             for bill_table_data in bill_table_data_list:  # 通过户号识别结果列表找到对应的识别内容
#                                 if str(bill_table_data[0]) == str(data['es_number']):
#                                     if bill_table_data[1] != '':
#                                         ocr_ele_loss = bill_table_data[1]['ele_loss']  # 电损值
#                             if ocr_ele_loss == '':
#                                 if str(page_electricity_loss).replace("[", "").replace("]", "").replace("'",
#                                                                                                         "") == '0':
#                                     audit_list.append([str(bill_number), '电损电量',
#                                                        '【' + str(data['es_number']) + '】' + str(page_electricity_loss),
#                                                        str(page_electricity_loss), '1', 'RPA', '合格',
#                                                        AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'),
#                                                        '4', ''])
#                                 else:
#                                     auditResult.auditStatus = '审核中'  # 转人工
#                                     audit_list.append(
#                                         [str(bill_number), '电损电量',
#                                          '【' + str(data['es_number']) + '】' + page_electricity_loss, str(ocr_ele_loss),
#                                          '0', 'RPA',
#                                          '附件2的【电损值】OCR识别为空',
#                                          AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
#                             elif round(float(ocr_ele_loss), 2) != round(
#                                     float(str(page_current_share_ratio).replace('%', '')) * float(
#                                         ocr_ele_loss) * 0.01, 2):
#                                 auditResult.auditStatus = '不通过'  # 返回稽核结果为不通过
#                                 audit_refused_type['信息与明细不符'] = 0
#                                 audit_list.append(
#                                     [str(bill_number), '电损电量', '【' + str(data['es_number']) + '】' + str(ocr_ele_loss),
#                                      str(
#                                          float(str(page_current_share_ratio).replace('%', '')) * float(
#                                              ocr_ele_loss) * 0.01), '0',
#                                      'RPA', '电损电量与电费明细不一致',
#                                      AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4',
#                                      ''])
#                             else:
#                                 audit_list.append(
#                                     [str(bill_number), '电损电量',
#                                      '【' + str(data['es_number']) + '】' + str(page_electricity_loss), str(
#                                         float(str(page_current_share_ratio).replace('%', '')) * float(
#                                             ocr_ele_loss) * 0.01), '1',
#                                      'RPA', '合格',
#                                      AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '4', ''])
#                         except Exception as e:
#                             # print(e)
#                             result['success'] = 1  # 返回稽核结果为转人工
#                             audit_list.append([str(bill_number), '电损电量',
#                                                '【' + str(data['es_number']) + '】' + str(page_electricity_loss),
#                                                str(page_current_share_ratio) + '% *(' + str(
#                                                    ocr_total_power) + '-' + str(
#                                                    ocr_now_display_num) + '-' + str(
#                                                    ocr_last_display_num) + ')',
#                                                '0', 'RPA', 'RPA处理异常',
#                                                AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'),
#                                                '4', ''])
#             except Exception as e:
#                 # print(e)
#                 result['success'] = 1  # 返回稽核结果为转人工
#                 audit_list.append([str(bill_number), '电损电量', '【' + str(data['es_number']) + '】' + '',
#                                    '0', 'RPA', 'RPA处理异常',
#                                    AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'),
#                                    '4', ''])
#     return auditResult, audit_list,audit_refused_type,result
"""
    规则11：页面【报账点核销信息】的【核销期始】和【核销期终】=附件2根据页面【电表缴费明细信息】-【户号】查找对应的【时间】/【起止时间】，如果附件2没有时间字段不作比对
                # ---    不一致后动作：【审核状态】-【不通过】，备注：缴费期始/终与电费明细不一致
"""
def audit_rule_eleven(auditResult, audit_list, audit_refused_type,bill_number,whether_execute,rar_file,
                      page_start_time, page_end_time,affix_list_from_db,ocr_start_date,ocr_end_date,
                      bill_table_data_list,verification_detail_info_data,file_flag2):
    if whether_execute == False:
        try:
            if '附件2' in rar_file:
                audit_list.append(
                    [str(bill_number), '核销期', str(page_start_time) + ',' + str(page_end_time), '', '0', 'RPA',
                     '附件2文件格式不正确', AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
            else:
                if not file_flag2:
                    audit_list.append(
                        [str(bill_number), '核销期', str(page_start_time) + ',' + str(page_end_time), '', '0', 'RPA',
                         '附件2电力明细清单不存在', AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
                else:
                    try:
                        if page_start_time and ocr_start_date and page_end_time and ocr_end_date:
                            start_date = datetime.datetime.strptime(ocr_start_date.replace(' ', ''), "%Y-%m-%d")
                            end_date = datetime.datetime.strptime(ocr_end_date.replace(' ', ''), "%Y-%m-%d")
                            ocr_start_date = str(start_date.strftime("%Y-%m-%d"))
                            ocr_end_date = str(end_date.strftime("%Y-%m-%d"))
                            start_date = start_date + datetime.timedelta(days=1)
                            end_date = end_date - datetime.timedelta(days=1)
                            start_date = start_date.strftime("%Y-%m-%d")
                            end_date = end_date.strftime("%Y-%m-%d")
                            if (str(page_start_time) == str(ocr_start_date).replace(' ', '')) and (
                                    str(page_end_time) == str(ocr_end_date).replace(' ', '')):
                                audit_list.append(
                                    [str(bill_number), '核销期', str(page_start_time) + ',' + str(page_end_time),
                                     str(ocr_start_date) + ',' + str(ocr_end_date), '1', 'RPA', '合格',
                                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '1', ''])
                            elif (str(page_start_time) == str(ocr_start_date)) and (
                                    str(page_end_time) == str(end_date)):
                                audit_list.append(
                                    [str(bill_number), '核销期', str(page_start_time) + ',' + str(page_end_time),
                                     str(ocr_start_date) + ',' + str(ocr_end_date), '1', 'RPA', '合格',
                                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '1', ''])
                            elif (str(page_start_time) == str(start_date)) and (
                                    str(page_end_time) == str(ocr_end_date)):
                                audit_list.append(
                                    [str(bill_number), '核销期', str(page_start_time) + ',' + str(page_end_time),
                                     str(ocr_start_date) + ',' + str(ocr_end_date), '1', 'RPA', '合格',
                                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '1', ''])
                            else:
                                auditResult.auditStatus = '不通过'  # 返回稽核结果为不通过
                                audit_refused_type['信息与明细不符'] = 0
                                audit_list.append(
                                    [str(bill_number), '核销期', str(page_start_time) + ',' + str(page_end_time),
                                     str(ocr_start_date) + ',' + str(ocr_end_date), '0', 'RPA', '核销期始/终与电费明细不一致',
                                     AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '1', ''])
                        else:
                            auditResult.auditStatus = '审核中'  # 转人工
                            audit_list.append([str(bill_number), '核销期', str(page_start_time) + ',' + str(page_end_time),
                                               str(ocr_start_date) + ',' + str(ocr_end_date), '0', 'RPA', '附件2起止时间为空',
                                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '1', ''])
                    except Exception as e:
                        # print(e)
                        auditResult.auditStatus = '审核中'  # 转人工
                        audit_list.append([str(bill_number), '核销期', str(page_start_time) + ',' + str(page_end_time),
                                           str(ocr_start_date) + ',' + str(ocr_end_date), '0', 'RPA', 'RPA处理异常',
                                           AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '1', ''])
        except Exception as e:
            # print(e)
            auditResult.auditStatus = '审核中'  # 转人工
            audit_list.append([str(bill_number), '核销期', '',
                               '', '0', 'RPA', 'RPA处理异常',
                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '1', ''])
    else:
        for data in verification_detail_info_data:
            try:
                if '附件2' in rar_file:
                    audit_list.append(
                        [str(bill_number), '核销期',
                         '【' + str(data['es_number']) + '】' + str(page_start_time) + ',' + str(page_end_time), '', '0',
                         'RPA', '附件2文件格式不正确', AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
                else:
                    if not file_flag2:
                        audit_list.append(
                            [str(bill_number), '核销期',
                             '【' + str(data['es_number']) + '】' + str(page_start_time) + ',' + str(page_end_time), '',
                             '0', 'RPA', '附件2电力明细清单不存在', AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'),
                             '5', ''])
                    else:
                        try:
                            ocr_start_date = ''
                            ocr_end_date = ''
                            for bill_table_data in bill_table_data_list:  # 通过户号识别结果列表找到对应的识别内容
                                if str(bill_table_data[0]) == str(data['es_number']):
                                    if bill_table_data[1] != '':
                                        ocr_start_date = bill_table_data[1]['start_date']  # 开始时间
                                        ocr_end_date = bill_table_data[1]['end_date']  # 结束时间
                            if page_start_time and ocr_start_date != '' and page_end_time and ocr_end_date != '':
                                start_date = datetime.datetime.strptime(ocr_start_date.replace(' ', ''),
                                                                        "%Y-%m-%d")
                                end_date = datetime.datetime.strptime(ocr_end_date.replace(' ', ''),
                                                                      "%Y-%m-%d")
                                ocr_start_date = str(start_date.strftime("%Y-%m-%d"))
                                ocr_end_date = str(end_date.strftime("%Y-%m-%d"))
                                start_date = start_date + datetime.timedelta(days=1)
                                end_date = end_date - datetime.timedelta(days=1)
                                start_date = start_date.strftime("%Y-%m-%d")
                                end_date = end_date.strftime("%Y-%m-%d")
                                if (str(page_start_time) == str(ocr_start_date)) and (
                                        str(page_end_time) == str(ocr_end_date)):
                                    audit_list.append(
                                        [str(bill_number), '核销期',
                                         '【' + str(data['es_number']) + '】' + str(page_start_time) + ',' + str(
                                             page_end_time),
                                         str(ocr_start_date) + ',' + str(ocr_end_date), '1', 'RPA', '合格',
                                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '1', ''])
                                elif (str(page_start_time) == str(ocr_start_date)) and (
                                        str(page_end_time) == str(end_date)):
                                    audit_list.append(
                                        [str(bill_number), '核销期',
                                         '【' + str(data['es_number']) + '】' + str(page_start_time) + ',' + str(
                                             page_end_time),
                                         str(ocr_start_date) + ',' + str(ocr_end_date), '1', 'RPA', '合格',
                                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '1', ''])
                                elif (str(page_start_time) == str(start_date)) and (
                                        str(page_end_time) == str(ocr_end_date)):
                                    audit_list.append(
                                        [str(bill_number), '核销期',
                                         '【' + str(data['es_number']) + '】' + str(page_start_time) + ',' + str(
                                             page_end_time),
                                         str(ocr_start_date) + ',' + str(ocr_end_date), '1', 'RPA', '合格',
                                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '1', ''])
                                else:
                                    auditResult.auditStatus = '不通过'  # 返回稽核结果为不通过
                                    audit_refused_type['信息与明细不符'] = 0
                                    audit_list.append(
                                        [str(bill_number), '核销期',
                                         '【' + str(data['es_number']) + '】' + str(page_start_time) + ',' + str(
                                             page_end_time),
                                         str(ocr_start_date) + ',' + str(ocr_end_date), '0', 'RPA',
                                         '核销期始/终与电费明细不一致',
                                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '1', ''])
                            else:
                                auditResult.auditStatus = '审核中'  # 转人工
                                audit_list.append([str(bill_number), '核销期',
                                                   '【' + str(data['es_number']) + '】' + str(
                                                       page_start_time) + ',' + str(page_end_time),
                                                   str(ocr_start_date) + ',' + str(ocr_end_date), '0',
                                                   'RPA', '附件2起止时间为空',
                                                   AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '1',
                                                   ''])
                        except Exception as e:
                            # print(e)
                            auditResult.auditStatus = '审核中'  # 转人工
                            audit_list.append(
                                [str(bill_number), '核销期',
                                 '【' + str(data['es_number']) + '】' + str(page_start_time) + ',' + str(page_end_time),
                                 str(ocr_start_date) + ',' + str(ocr_end_date), '0', 'RPA', 'RPA处理异常',
                                 AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '1', ''])
            except Exception as e:
                # print(e)
                auditResult.auditStatus = '审核中'  # 转人工
                audit_list.append([str(bill_number), '核销期', '【' + str(data['es_number']) + '】' + '',
                                   '', '0', 'RPA', 'RPA处理异常',
                                   AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '1', ''])
    return auditResult, audit_list, audit_refused_type